import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
      token:uni.getStorageSync('token'),
		userInfo: uni.getStorageSync("user")? JSON.parse(uni.getStorageSync("user")):false,
    faceBaceUrl:uni.getStorageSync("faceBaceUrl")? uni.getStorageSync("faceBaceUrl"):'',
    subjectList: uni.getStorageSync("subjectList")? uni.getStorageSync("subjectList"):[],//学科列表
    testModel:uni.getStorageSync("testModel")?uni.getStorageSync("testModel"):{total:0,testList:[]},//试卷列表{总数，试卷，视频}
    isConnected:false,//是否连网
    doTopicList:uni.getStorageSync("doTopicList")? uni.getStorageSync("doTopicList"):[], //做题试题列表
    userTaskInfo:uni.getStorageSync("userTaskInfo")? uni.getStorageSync("userTaskInfo"):[], //用户做题记录
    wrongModels:uni.getStorageSync("wrongModels")?uni.getStorageSync("wrongModels"):{} //错题集列表
	},
    mutations: {
		setUserInfo(state,data){
      state.userInfo =uni.getStorageSync("user")? JSON.parse(uni.getStorageSync("user")):data
		},
    setFaceBaceUrl(state,data)
    {
       uni.setStorageSync('faceBaceUrl',data)
      state.faceBaceUrl=data
    },
    setSubjectList(state,data){
      uni.setStorageSync('subjectList',data)
      state.subjectList=data
    },
    setTestModel(state,data){
      data.testList.forEach(item=>{
        let index=state.testModel.testList.findIndex(obj=>obj.id===item.id)
        if(index<0){
          state.testModel.testList.push(item)
        }
      })
      state.testModel.total=data.total
      uni.setStorageSync('testModel',state.testModel)
    },
    setWrongModels(state,data){
      data.forEach(item=>{
        let index=state.wrongModels.findIndex(obj=>obj.disciplineId===item.disciplineId)
        if(index<0){
          state.wrongModels.push(item)
        }
      })
      uni.setStorageSync('wrongModels',state.wrongModels)
    },
    setIsConnected(state,data){
      state.isConnected=data
    },
    setDoTopicList(state,list){
       // 先筛选用户是否做过题
         let index = state.doTopicList.findIndex(item=>item.id===list[0].id)
         if(index<0)
         {//未保存 添加
           state.doTopicList.push(list[0])
         }else
         {
           //删除操作
           if (list[1] === 'del') {
             state.doTopicList.splice(index,1)
           } else {
           //已保存 替换
           state.doTopicList.splice(index,1,list[0])
           }
         }
       
       uni.setStorageSync('doTopicList',state.doTopicList)
    },
    setUserTaskInfo(state,list){
      // 先筛选用户是否做过题
        let index = state.userTaskInfo.findIndex(item=>item.id===list[0].id)
        if(index<0)
        {//未做题 添加
          state.userTaskInfo.push(list[0])
        }else
        {
          //删除操作
          if (list[1] === 'del') {
            state.userTaskInfo.splice(index,1)
          } else {
          //已做过 替换
          state.userTaskInfo.splice(index,1,list[0])
          }
        }
      
      uni.setStorageSync('userTaskInfo',state.userTaskInfo)
    }
   
	},
    actions: {
     async setSubjectListAsync({commit,state}){
       console.log(state.userInfo)
          // console.log(state.token)
      let res=await Vue.prototype.$http.get('/discipline/list', {
          params: {
            areaId: state.userInfo.areaId,
            education: state.userInfo.education,
            size: 100,
            page: -1
          }
        })
       // console.log(res)
       commit('setSubjectList',res.data.data)
        // uni.request({
        //   url:`${this.$baseUrl}/discipline/list`,
        //   method:"GET",
        //   data:{
        //     areaId: state.userInfo.areaId,
        //     education: state.userInfo.education,
        //     size: 100,
        //     page: -1
        //   },
        //   success:(res)=> {
        //      commit('setSubjectList',res.data.data)
        //   }
        // })
      }
	}
})
export default store